package com.imooc.miaosha.rabbitmq;

import com.imooc.miaosha.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Paul
 * @version 0.1
 * @date 2018/3/22 0022
 */
@Service
@Slf4j
public class MQSender {

    private final AmqpTemplate template;

    @Autowired()
    public MQSender(AmqpTemplate template) {
        this.template = template;
    }


//    public void send(Object msg) {
//        String message = RedisService.beanToString(msg);
//        template.convertAndSend(MQConfig.QUEUE, message);
//        log.info("发送消息=" + message);
//    }
//
//    public void sendTopic(Object msg) {
//        String message = RedisService.beanToString(msg);
//        log.info("发送消息=" + message);
//        template.convertAndSend("topic_exchange", "topic.key1", message + "1");
//        template.convertAndSend("topic_exchange", "topic.key2", message + "2");
//    }
//
//    public void sendFanout(Object msg) {
//        String message = RedisService.beanToString(msg);
//        log.info("发送消息=" + message);
//        template.convertAndSend("fanout_exchange", "", message);
//    }
//
//    public void sendHeaders(Object msg) {
//        String message = RedisService.beanToString(msg);
//        log.info("发送消息=" + message);
//        MessageProperties messageProperties = new MessageProperties();
//        //放入两个herder才能识别匹配
//        messageProperties.setHeader("header1", "value1");
//        messageProperties.setHeader("header2", "value2");
//        Message message1 = new Message(message.getBytes(), messageProperties);
//        template.convertAndSend("headers_exchange", "", message1);
//    }

    public void sendMiaoshaMessage(MiaoshaMessage miaoshaMessage) {
        String message = RedisService.beanToString(miaoshaMessage);
        template.convertAndSend(MQConfig.MIAOSHA_QUEUE, message);
        log.info("发送消息=" + message);
    }
}
